using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudentMgmt.IDAL;
using StudentMgmt.Model;
using StudentMgmt.Model.DataContext;
using System.Data;
using System.Data.SqlClient;
using StudentMgmt.DBUtility;
using System.Windows.Forms;

namespace StudentMgmt.SQLServerDAL
{
   public  class Changes : IChange
    {
      
       public  ChangeInfo GetLastChangeByStuNo(string Stu_No)
        {
            StudentMgmtDataContext db = new StudentMgmtDataContext();
            var temp = (from m in db.Changes
                        where m.StuNo == Stu_No
                        orderby m.Create_Date descending
                        select m);
            if (temp.ToList().Count!=0)
            {
                var tmp = temp.First();
                return tmp;
            }
            else return null;  
        }

       public void InsertChange(ChangeInfo  changeInfo)
       {
           StudentMgmtDataContext db = new StudentMgmtDataContext();
           db.Changes.InsertOnSubmit(changeInfo);
           db.SubmitChanges();
       }

       public int QueryChanges(string Stu_No, out DataTable objTable)
       {
           int val = 0;
           //objTable = new DataTable();
           StringBuilder builder = new StringBuilder();
           builder.Append("select chg.StuNo as '学号',chgConfig.Change_Descriptions as'变更类型',chg.Create_Date  as '创建日期', chg.Change_Descriptions as '说明'");
           builder.Append(" from Change as chg inner join Change_Config as chgConfig on chg.Change_Code=chgConfig.Change_Code");
           builder.Append(" where chg.StuNo='"+Stu_No+"';");
           val = SqlHelper.RunSQLReturnDataTable(builder.ToString() , out objTable);
           return val;
       }

       public  string GetClass_Id(string Stu_No)
       {
           string str;
           StringBuilder builder = new StringBuilder();
           builder.Append("select  ClassId from Student where StuNo ='"+Stu_No+"';");
           str = SqlHelper.RunSQLReturnString(builder.ToString());
           return str;
       }
    }
}
